package com.huqu.beans;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.NamedEvent;

import com.huqu.dao.Q100Dao;
import com.huqu.entities.Q100;
import com.huqu.utils.ApplicationConstant;
import com.huqu.utils.ApplicationHelper;

@ManagedBean
@ViewScoped
public class Q100Bean implements Serializable {

	private static final long serialVersionUID = -1931369229610574991L;
	
	private String hello="xin chĂ o táº¥t cáº£ má»�i ngÆ°á»�i.";
	private String username;
	private String password;
	private String message;
	private String strlocale;
	private String test;
	private String lang="VN";
	private String para=null;
	
	private List<Q100> listq100=new ArrayList<Q100>();
	
	@PostConstruct
	public void init(){
		System.out.println("-------- Q100Bean() ----------");
		/*if(ApplicationHelper.getRequest().getParameter("lang")!=null){
			lang=ApplicationHelper.getRequest().getParameter("lang").toString();
		}else{
			lang="VN";
		}
		String para1 = ApplicationHelper.getParameter().get("para1");
		System.err.println("---- para1= "+para1);
		ApplicationHelper.loadLanguage();*/

	}
	
	public Q100Bean(){}
	
	public void checklogin(){
		try {
			System.out.println("-------- Q100Bean.test() ----------");
			Q100Dao q100Dao = (Q100Dao) ApplicationHelper.findBean(ApplicationConstant.REPOSITORY_NAME_Q100);
			int result=0;
			result=q100Dao.Checklogin(username, password);
			System.out.println("----- result= "+result);
			if(result>0){
				ApplicationHelper.showMessageSuccess("Đăng nhập thành công.");
				message="Đăng nhập thành công.";
				//ApplicationHelper.redirectTo("home_page.xhtml");
				ApplicationHelper.gotoPage("home1");
				/*getlistQ100();
				for(Q100 q100:listq100){
					System.err.println(q100.getTendangnhap()+" --- "+q100.getMatkhau());
				}*/
			}else{
				ApplicationHelper.showMessageErro("Đăng nhập thất bại.");
				message="Đăng nhập thất bại.";
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void getlistQ100(){
		try {
			System.out.println("-------- Q100Bean.test() ----------");
			Q100Dao q100Dao = (Q100Dao) ApplicationHelper.findBean(ApplicationConstant.REPOSITORY_NAME_Q100);
			listq100=q100Dao.ListOfTabQ100(0, 10);
			System.out.println("--- listQ100.size()= "+listq100.size());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void change_language(String strlocale1){
		try {
			if(strlocale1.trim().equals("en")||strlocale1.trim().equals("EN")){
				FacesContext.getCurrentInstance()
    			.getViewRoot().setLocale(Locale.US);
			}else{
				Locale locale=new Locale("vi", "VN");
				FacesContext.getCurrentInstance()
    			.getViewRoot().setLocale(locale);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public String getHello() {
		return hello;
	}
	public void setHello(String hello) {
		this.hello = hello;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public String getStrlocale() {
		return strlocale;
	}

	public void setStrlocale(String strlocale) {
		this.strlocale = strlocale;
	}

	public String getTest() {
		Map<String,String> params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
		String action = params.get("page");
		test= action;
		return test;
	}

	public void setTest(String test) {
		this.test = test;
	}

	public String getLang() {
		return lang;
	}

	public void setLang(String lang) {
		this.lang = lang;
	}

	public String getPara() {
		return para;
	}

	public void setPara(String para) {
		this.para = para;
	}
	
	
}
